package IOTest.exercise;

import java.io.*;
import java.util.Scanner;

//进⾏普通⽂件的复制
public class Two {
    public static void main(String[] args) {
        //1、输入源文件路径和目标文件路径
        Scanner scanner = new Scanner(System.in);
        System.out.println("需要复制的文件路径：");
        String recPath = scanner.next();
        System.out.println("要复制到的目的路径：");
        String destPath = scanner.next();

        //2、判断路径的合法性
        File recFile = new File(recPath);
        if(!recFile.isFile()){
            System.out.println("源文件路径错误！");
            return;
        }
        File destFile = new File(destPath);
        File destParent = new File(destFile.getParent());
        if(!destParent.isDirectory()){
            System.out.println("目标文件路径错误！");
            return;
        }

        //3、开始复制内容
        try(InputStream inputStream = new FileInputStream(recPath); OutputStream outputStream = new FileOutputStream(destPath)){
            while (true){
                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                if(n == -1){
                    break;
                }
                outputStream.write(bytes,0,n);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
